<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动物识别</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            padding: 20px;
            background: #f8f9fa;
            color: #333;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
        }
        h2 {
            color: #2c3e50;
            margin-bottom: 30px;
            text-align: center;
            font-size: 24px;
        }
        .test-container {
            display: flex;
            gap: 30px;
            margin-top: 30px;
        }
        .left-panel, .right-panel {
            flex: 1;
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            border: 1px solid #e1e1e1;
        }
        .test-form {
            margin-bottom: 20px;
            display: flex;
            gap: 10px;
            align-items: center;
        }
        .file-input-wrapper {
            position: relative;
            overflow: hidden;
            display: inline-block;
        }
        .file-input-wrapper input[type=file] {
            font-size: 100px;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            cursor: pointer;
        }
        .file-input-trigger {
            background: #f8f9fa;
            border: 1px solid #ddd;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            display: inline-block;
        }
        .test-btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 8px 20px;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.3s;
        }
        .test-btn:hover {
            background: #2980b9;
        }
        .preview-area {
            margin: 20px 0;
            text-align: center;
            min-height: 200px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #f8f9fa;
            border-radius: 8px;
            border: 2px dashed #ddd;
        }
        .preview-area img {
            max-width: 100%;
            max-height: 300px;
            border-radius: 4px;
            display: none;
        }
        .result-area {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            min-height: 200px;
            font-family: "Microsoft YaHei", sans-serif;
            font-size: 14px;
            line-height: 1.6;
            overflow: auto;
        }
        .result-list {
            padding: 0;
        }
        .result-item {
            margin-bottom: 20px;
            padding: 15px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .result-item p {
            margin: 8px 0;
            padding-left: 10px;
            border-left: 3px solid #e8e8e8;
        }
        .result-item .title {
            font-size: 16px;
            color: #2c3e50;
            font-weight: bold;
            margin-bottom: 15px;
            padding-left: 0;
            border-left: none;
        }
        .result-item .name {
            border-left-color: #3498db;
        }
        .result-item .score {
            border-left-color: #2ecc71;
        }
        .result-item .baike {
            margin-top: 15px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 4px;
            border-left: none;
        }
        .result-item .baike-desc {
            color: #666;
            line-height: 1.8;
            margin: 10px 0;
        }
        .result-item a {
            color: #3498db;
            text-decoration: none;
            display: inline-block;
            margin-top: 5px;
        }
        .result-item a:hover {
            text-decoration: underline;
        }
        .error {
            color: #e74c3c;
            padding: 10px;
            background: #fdf3f2;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>动物识别</h2>
        <div class="test-container">
            <div class="left-panel">
                <h3 class="panel-title">图片上传</h3>
                <div class="test-form">
                    <div class="file-input-wrapper">
                        <div class="file-input-trigger">选择图片</div>
                        <input type="file" id="testImage" accept="image/*" onchange="previewImage(event)">
                    </div>
                    <button onclick="testAPI()" class="test-btn">开始识别</button>
                </div>
                <div class="options">
                    <label>
                        返回结果数量：
                        <input type="number" id="topNum" value="6" min="1" max="10">
                    </label>
                    <br>
                    <label>
                        百科结果数量：
                        <input type="number" id="baikeNum" value="1" min="0" max="5">
                    </label>
                </div>
                <div class="preview-area">
                    <img id="preview" alt="预览图">
                </div>
            </div>
            <div class="right-panel">
                <h3 class="panel-title">识别结果</h3>
                <div id="result" class="result-area"></div>
            </div>
        </div>

        <!-- API文档部分 -->
        <div class="api-docs">
            <details>
                <summary>API 文档</summary>
                <div class="api-section">
                    <h4>接口描述</h4>
                    <p>该接口用于识别图片中的动物，返回动物名称、置信度和相关百科信息。</p>
                </div>
                
                <div class="api-section">
                    <h4>请求URL</h4>
                    <pre><code>POST /animal/process</code></pre>
                </div>
                
                <div class="api-section">
                    <h4>请求参数</h4>
                    <table>
                        <tr>
                            <th>参数名</th>
                            <th>必选</th>
                            <th>类型</th>
                            <th>说明</th>
                        </tr>
                        <tr>
                            <td>image</td>
                            <td>和url二选一</td>
                            <td>file</td>
                            <td>图片文件，base64编码后大小不超过4M，最短边至少15px，最长边最大4096px，支持jpg/png/bmp格式</td>
                        </tr>
                        <tr>
                            <td>url</td>
                            <td>和image二选一</td>
                            <td>string</td>
                            <td>图片完整URL，长度不超过1024字节</td>
                        </tr>
                        <tr>
                            <td>top_num</td>
                            <td>否</td>
                            <td>integer</td>
                            <td>返回预测得分top结果数，默认为6</td>
                        </tr>
                        <tr>
                            <td>baike_num</td>
                            <td>否</td>
                            <td>integer</td>
                            <td>返回对应数量的百科信息，默认不返回</td>
                        </tr>
                    </table>
                </div>
                
                <div class="api-section">
                    <h4>返回参数</h4>
                    <table>
                        <tr>
                            <th>参数名</th>
                            <th>类型</th>
                            <th>说明</th>
                        </tr>
                        <tr>
                            <td>success</td>
                            <td>boolean</td>
                            <td>请求是否成功</td>
                        </tr>
                        <tr>
                            <td>result.log_id</td>
                            <td>uint64</td>
                            <td>唯一的日志ID</td>
                        </tr>
                        <tr>
                            <td>result.result</td>
                            <td>array</td>
                            <td>识别结果数组</td>
                        </tr>
                        <tr>
                            <td>result.result[].name</td>
                            <td>string</td>
                            <td>动物名称</td>
                        </tr>
                        <tr>
                            <td>result.result[].score</td>
                            <td>float</td>
                            <td>置信度</td>
                        </tr>
                        <tr>
                            <td>result.result[].baike_info</td>
                            <td>object</td>
                            <td>百科信息</td>
                        </tr>
                    </table>
                </div>
                
                <div class="api-section">
                    <h4>返回示例</h4>
                    <pre><code>{
    "success": true,
    "result": {
        "log_id": "7357875020059572776",
        "result": [{
            "name": "蒙古马",
            "score": 0.5321,
            "baike_info": {
                "baike_url": "http://baike.baidu.com/...",
                "image_url": "http://imgsrc.baidu.com/...",
                "description": "蒙古马是世界著名的古老马种..."
            }
        }]
    }
}</code></pre>
                </div>
                
                <div class="api-section">
                    <h4>Python调用示例</h4>
                    <pre><code>import requests

# 上传图片文件
files = {'image': open('image.jpg', 'rb')}
data = {
    'top_num': 6,
    'baike_num': 1
}
response = requests.post('http://your-domain/animal/process', files=files, data=data)

# 处理响应
if response.status_code == 200:
    result = response.json()
    if result['success']:
        # 处理识别结果
        animals = result['result']['result']
        for animal in animals:
            print(f"动物: {animal['name']}, 置信度: {animal['score']}")
            if 'baike_info' in animal:
                print(f"百科描述: {animal['baike_info']['description']}")
    else:
        print(result['message'])</code></pre>
                </div>
            </details>
        </div>
    </div>

    <script>
        function previewImage(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    const preview = document.getElementById('preview');
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                    document.getElementById('result').textContent = '';
                }
                reader.readAsDataURL(file);
            }
        }

        function formatResult(data) {
            if (!data.success) {
                return `<div class="error">${data.message}</div>`;
            }

            const results = data.result.result;
            let html = '<div class="result-list">';
            
            results.forEach((item, index) => {
                html += `
                    <div class="result-item">
                        <p class="title">识别结果 ${index + 1}</p>
                        <p class="name">动物名称：${item.name}</p>
                        <p class="score">置信度：${(item.score * 100).toFixed(2)}%</p>
                        ${item.baike_info ? `
                            <p class="baike">
                                <strong>百科信息：</strong>
                                <div class="baike-desc">${item.baike_info.description || '暂无描述'}</div>
                                ${item.baike_info.baike_url ? `
                                    <a href="${item.baike_info.baike_url}" target="_blank">
                                        查看百科详情 <i class="fas fa-external-link-alt"></i>
                                    </a>
                                ` : ''}
                            </p>
                        ` : ''}
                    </div>
                `;
            });
            
            html += '</div>';
            return html;
        }

        async function testAPI() {
            const fileInput = document.getElementById('testImage');
            const file = fileInput.files[0];
            if (!file) {
                alert('请先选择图片');
                return;
            }

            const formData = new FormData();
            formData.append('image', file);
            formData.append('top_num', document.getElementById('topNum').value);
            formData.append('baike_num', document.getElementById('baikeNum').value);

            try {
                document.getElementById('result').textContent = '识别中...';
                const response = await fetch('/animal/process', {
                    method: 'POST',
                    body: formData
                });

                const data = await response.json();
                document.getElementById('result').innerHTML = formatResult(data);
                
            } catch (error) {
                document.getElementById('result').textContent = `请求错误：${error.message}`;
            }
        }
    </script>
</body>
</html> 